/**
 * File:   NetDataModel.java
 * Author: Gvennet
 */
package scenarios.net;

import dht.identifier.identifier.Identifier;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * todo write javadoc
 */
public final class NetDataModel
{

	private final List<NetDataRow> data;

	private final List<NetDataEventListener> listenerListData;


	public NetDataModel()
	{
		data = new LinkedList<NetDataRow>();
		listenerListData = new CopyOnWriteArrayList<NetDataEventListener>();
	}

	public void put(NetDataRow dataRow)
	{

		if (!data.contains(dataRow))
		{
			data.add(dataRow);
			fireRowInserted(dataRow);
		}
	}


	public List<NetDataRow> get(Identifier valueId)
	{

		List<NetDataRow> filter = new ArrayList<NetDataRow>();

		for (NetDataRow aData : data)
		{

			if (valueId.equals(aData.getDataId()))
			{
				filter.add(aData);
			}
		}

		return filter;
	}

	public List<NetDataRow> getData()
	{
		return new LinkedList<NetDataRow>(data);
	}

	public void addListener(NetDataEventListener listenerData)
	{
		listenerListData.add(listenerData);
	}

	public void removeListener(NetDataEventListener listenerData)
	{
		listenerListData.remove(listenerData);
	}


	private void fireRowInserted(NetDataRow dataRow)
	{
		for (NetDataEventListener aListenerListData : listenerListData)
		{
			aListenerListData.rowAdded(dataRow);
		}
	}

	private void fireRowRemoved(NetDataRow dataRow)
	{
		for (NetDataEventListener aListenerListData : listenerListData)
		{
			aListenerListData.rowRemoved(dataRow);
		}
	}
}